__inline("./lib/util.js");
$(function() {
  var app = {
    states: {
      rd: {},
      idCard: "",
      errPost: true,
      ___: "",
      vObj: {
        idCard: /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/,
        n6: /^\d{6}$/,
        num: /^[0-9]+([.]{1}[0-9]+){0,1}$/,
        phone: /1\d{10}/,
      }
    },
    _szct(str) {
      str = "SZCT" + str;
      return Base64.encode(str);
    },
    mkLine(str) {
      let jqStr = "#" + str;
      $(jqStr).css("border-color", "#fff").addClass("errLine");
    },
    reLine() {
      $(".lotto_text").css("border-color", "#DA251D").removeClass("errLine")
    },
    validate() {
      let vd = this.states.rd;
      let vo = this.states.vObj;
      this.states.errPost = true;
      this.reLine();
      // 竞彩足球
      "123456".match(/^\d{6}$/);
      if (!vd.jczq || !vd.jczq.match(vo.n6)) {
        swal("格式错误", "彩票序列号填写错误，请重新输入！", "error");
        this.mkLine("lotto_football");
        return
      }
      // 大乐透
      if (!vd.dlt || !vd.dlt.match(vo.n6)) {
        swal("格式错误", "彩票序列号填写错误，请重新输入！", "error");
        this.mkLine("lotto_lotto");
        return
      }
      // 总金额
      if (!vd.amount || !vd.amount.match(vo.num)) {
        swal("格式错误", "总金额填写错误，请重新输入！", "error");
        this.mkLine("lotto_money");
        return
      }
      // 姓名
      if (vd["name"] === "") {
        swal("格式错误", "姓名不能为空，请重新输入！", "error");
        this.mkLine("lotto_name");
        return
      }
      // 身份证号
      if (!this.states.idCard || !this.states.idCard.match(vo.idCard)) {
        swal("格式错误", "身份证号填写错误，请重新输入！", "error");
        this.mkLine("lotto_idnum");
        return
      }
      // 手机号
      if (!vd.phone || !vd.phone.match(vo.phone)) {
        swal("格式错误", "手机号填写错误，请重新输入！", "error");
        this.mkLine("lotto_phone");
        return
      }
      // 验证码
      if (vd.imageCode.length != 4) {
        swal("格式错误", "验证码填写错误，请重新输入！", "error");
        this.mkLine("lotto_code");
        return
      }
      this.states.errPost = false;
    },
    getInfo() {
      let self = this;
      let rd = {
        format: "json",
        ___: self.states.___,
      };
      rd.dlt = $("#lotto_lotto").val();
      rd.jczq = $("#lotto_football").val();
      rd.amount = $("#lotto_money").val();
      rd["name"] = $("#lotto_name").val();
      rd.idCard = $("#lotto_idnum").val();
      rd.phone = $("#lotto_phone").val();
      // rd = {
      //   dlt: 123456,
      //   jczq: 123456,
      //   amount: 123456,
      //   name: "lanbos",
      //   idCard: 150204198911240917,
      //   format: "json",
      //   phone: 18516891422,
      //   ___: self.states.___,
      // }
      this.states.idCard = rd.idCard;
      rd.imageCode = $("#lotto_code").val();
      rd.idCard = this._szct(rd.idCard);
      rd.___ = this._szct(rd.___);
      this.states.rd = rd;
    },
    setInfo() {
      this.getInfo();
      this.validate();
      if (this.states.errPost) {
        return
      }
      this.states.rd.__isTest=1;
      util.paramMd5(this.states.rd);
      let obj = {
        cat1: "activity",
        data: this.states.rd
      }
      let sinaAJax = new util.SinaAJax(obj);
      sinaAJax._post();
      sinaAJax.next((data) => {
        swal("提交成功！", "", "success");
        this.getImgCode();
        let ctime = 30 * 60;
        util.cookie.setItem("ct_idCard", this.states.idCard, "", ctime);
        util.cookie.setItem("ct_name", this.states.rd.name, "", ctime);
        util.cookie.setItem("ct_phone", this.states.rd.phone, "", ctime);
        console.log(data);
      })
    },
    getImgCode() {
      let obj = {
        cat1: "getImageCode",
        data: {
          format: "json",
          imageH: 37,
          imageW: 100,
          fontSize: 14,
          __isTest:1
        }
      };
      let sinaAJax = new util.SinaAJax(obj);
      sinaAJax.dataType = "jsonp";
      sinaAJax.next((data) => {
        this.states.___ = data.data.___;
        $("#lotto_imagecode_img")
          .attr("src", data.data.image);
      });
    },
    bindEvent() {
      var self = this;
      $("body").on("focus", ".errLine", function() {
        self.reLine();
      });
      $(".lotto_submit").click(function() {
        self.setInfo();
      });
      $("#lotto_imagecode_img").click(function() {
        self.getImgCode();
      })
    },
    run() {
      let ct_id = util.cookie.getItem("ct_idCard") || "";
      let ct_phone = util.cookie.getItem("ct_phone") || "";
      let ct_name = util.cookie.getItem("ct_name") || "";
      $("#lotto_idnum").val(ct_id);
      $("#lotto_phone").val(ct_phone);
      $("#lotto_name").val(ct_name);
      this.getImgCode();
      this.bindEvent();
    }
  }
  app.run();
});